﻿using Jayrock.Json;
using System;
using System.Collections;
using System.Collections.Generic;

namespace open_api_sdk_csharp.com.chinaums.open.api.internals.utils.convertors
{
    class OpenApiReader : IOpenApiReader
    {
        private IDictionary json;
        public OpenApiReader(IDictionary json) { this.json = json; }
        public IList GetListObjects(string listName, string itemName, Type type, DOpenApiConvert convert)
        {
            IList listObjs = null;
            Object jsonObject = json[listName];
            IList jsonList = null;
            if (jsonObject is IList)
            {
                jsonList = jsonObject as IList;
            }
            else if (jsonObject is IDictionary)
            {
                IDictionary jsonMap = jsonObject as IDictionary;
                if (jsonMap != null && jsonMap.Count > 0)
                {
                    Object itemTmp = jsonMap[itemName];
                    if (itemTmp == null && listName != null)
                    {
                        itemTmp = jsonMap[listName.Substring(0, listName.Length - 1)];
                    }
                    if (itemTmp is IList)
                    {
                        jsonList = itemTmp as IList;
                    }
                }
            }
            if (jsonList != null && jsonList.Count > 0)
            {
                Type listType = typeof(List<>).MakeGenericType(new Type[] { type });
                listObjs = Activator.CreateInstance(listType) as IList;
                foreach (object item in jsonList)
                {
                    if (typeof(IDictionary).IsAssignableFrom(item.GetType()))
                    {
                        IDictionary subMap = item as IDictionary;
                        object subObj = convert(new OpenApiReader(subMap), type);
                        if (subObj != null)
                        {
                            listObjs.Add(subObj);
                        }
                    }
                    else if (typeof(IList).IsAssignableFrom(item.GetType()))
                    {
                    }
                    else if (typeof(JsonNumber).IsAssignableFrom(item.GetType()))
                    {
                        JsonNumber jsonNumber = (JsonNumber)item;
                        if (typeof(long).IsAssignableFrom(type))
                        {
                            listObjs.Add(jsonNumber.ToInt64());
                        }
                        else if (typeof(int).IsAssignableFrom(type))
                        {
                            listObjs.Add(jsonNumber.ToInt32());
                        }
                        else if (typeof(double).IsAssignableFrom(type))
                        {
                            listObjs.Add(jsonNumber.ToDouble());
                        }
                    }
                    else 
                    {
                        listObjs.Add(item);
                    }
                }

            }
            return listObjs;
        }
        public object GetPrimitiveObject(object name)
        {
            return json[name];
        }
        public object GetReferenceObject(object name, Type type, DOpenApiConvert convert)
        {
            IDictionary dict = json[name] as IDictionary;
            if (dict != null && dict.Count > 0)
            {
                return convert(new OpenApiReader(dict), type);
            }
            else
            {
                return null;
            }
        }
        public bool HasReturnField(object name)
        {
            return json.Contains(name);
        }
    }
}
